package org.uim.cricket.entities.parser.impl;

import java.util.HashSet;

import org.apache.commons.lang3.StringEscapeUtils;
import org.uim.cricket.entities.FullSeries;
import org.uim.cricket.entities.parser.SeriesEntityParsable;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

public class HTMLSeriesPageParser implements SeriesEntityParsable {
	
	private String[] drawnCaptions = {"Drawn", "Shared"};
	
	@Override
	public FullSeries parseSeries(StringBuilder source) {
		FullSeries series = new FullSeries();
		
		int pos = source.indexOf("<!-- START 650 AREA -->");
		if (pos < 0) return null;
		
		String src = source.substring(pos + "<!-- START 650 AREA -->".length());
		src = src.replaceAll("&nbsp;", "");
		
		String header = MyStringUtils.inBetween(src, "<h3", "</h3>");
		if (header != null) {
			
			header = "<h3" + header;
			header = StringEscapeUtils.unescapeHtml4(header);
			header = HTMLStringUtils.cleanHTMLTags(header);
			
			pos = header.indexOf(" - ");
			if (pos > 0) {
				header = header.substring(pos + " - ".length()).trim();
			}
			
			if (header.endsWith(")")) {
				int spos = header.lastIndexOf("(");
				int epos = header.lastIndexOf(")");
				String matches = header.substring(spos + 1, epos).trim();
				int tmatches = Integer.parseInt(matches);
				series.setTotalMatches(tmatches);
				
				epos = spos;
				spos = header.lastIndexOf('-', spos - 1);
				if (spos > 0) {
					do {
						spos--;
						if (spos == 0) {
							spos = -1;
							break;
						}
					} while (header.charAt(spos) >= '0' && header.charAt(spos) <= '9');
					
					if (spos > 0) {
						series.setMargin(header.substring(spos + 1, epos).trim());
						header = header.substring(0, spos).trim();
					}
				} else {
					header = header.substring(0, epos).trim();
				}
			}
			
			if (this.drawnCaptions == null) {
				this.drawnCaptions = new String[] {"Drawn", "Shared"};
			}
			
			series.setDrawn(false);
			for (int i=0; i<this.drawnCaptions.length; i++) {
				if (header.startsWith(this.drawnCaptions[i])) {
					series.setDrawn(true);
					break;
				}
			}
			
			if (!series.isDrawn()) {
				series.setTeamWon(header);
			}
			
		}
		
		String[] matches = MyStringUtils.splitInBetween(src, "<span class=\"potMatchLink\">", "</span>");
		if (matches == null) return series;
		
		int tot = 0;
		HashSet<String> allteams = new HashSet<String>();
		for (int i=0; i<matches.length; i++) {
			String smatch = HTMLStringUtils.cleanHTMLTags(matches[i]);
			smatch = StringEscapeUtils.unescapeHtml4(smatch).trim();
			
			tot++;
			int sp = smatch.indexOf(" v ");
			if (sp < 0) continue;
			int ep = smatch.indexOf(" at ", sp + 3);
			
			String team1 = smatch.substring(0, sp).trim();
			String team2 = null;
			if (ep < 0) {
				team2 = smatch.substring(sp + 3).trim();
			} else {
				team2 = smatch.substring(sp + 3, ep).trim();
			}
			
			allteams.add(team1);
			allteams.add(team2);
		}
		
		series.setParticipatedCountries(allteams);
		series.setTotalMatches(tot);
		
		return series;
	}
	
	public void setDrawnCaptions(String[] drawnCaptions) {
		this.drawnCaptions = drawnCaptions;
	}
	
	public String[] getDrawnCaptions() {
		return drawnCaptions;
	}
	

}
